package com.css.fxfzypg.modules.baseData.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.base.user.service.UserService;
import com.css.fxfzypg.modules.baseData.entity.BuildingVulnerability;
import com.css.fxfzypg.modules.baseData.service.BuildingVulnerabilityService;
import com.css.fxfzypg.modules.baseData.queryParams.BuildingVulnerabilityQueryParams;
import com.css.fxfzypg.modules.system.service.UserCacheService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-11 14:45
 * 建筑物易损性
 */
@RestController
@RequestMapping("/buildingVulnerability")
@Slf4j
public class BuildingVulnerabilityController {

    @Resource
    BuildingVulnerabilityService buildingVulnerabilityService;

    @Autowired
    UserCacheService userCacheService;

    /**
     * 查询
     *
     * @param queryParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(BuildingVulnerabilityQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            List<BuildingVulnerability> page = buildingVulnerabilityService.getPage(queryParams);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param list
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody List<BuildingVulnerability> list) {
        RestResponse restResponse = null;
        try {
           /* String currUid = PlatformSessionContext.getUserID();
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            switch (userCacheService.getUserRoleType(currUid)) {
                case DATA_ROLES_PROVINCE:
                case DATA_ROLES_OTHER:
                    return RestResponse.fail("国家级管理员才可以进行修改");
                default:
                    break;
            }*/
            buildingVulnerabilityService.update(list);
            restResponse = RestResponse.succeed("修改成功");
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    @GetMapping("/query")
    public RestResponse query(BuildingVulnerabilityQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            List<BuildingVulnerability> page = buildingVulnerabilityService.query(queryParams);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存
     *
     * @param list
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody List<BuildingVulnerability> list) {
        RestResponse restResponse = null;
        try {
            buildingVulnerabilityService.save(list);
            restResponse = RestResponse.succeed("保存成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if (StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 通过设防烈度获取基础数据子系统的数据
     *
     * @param sfIntensity
     * @return
     */
    @GetMapping("/getBySfIntensity")
    public RestResponse getBySfIntensity(@RequestParam String sfIntensity) {
        RestResponse restResponse = null;
        try {
            restResponse = buildingVulnerabilityService.getBySfIntensity(sfIntensity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导入矩阵
     *
     * @param file
     * @return
     */
    @PostMapping("/importVulnerability")
    public RestResponse importVulnerability(MultipartFile file) {
        RestResponse restResponse = null;
        try {
            restResponse = buildingVulnerabilityService.importVulnerability(file);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if (StringUtils.isBlank(errormessage)) {
                errormessage = "导入矩阵失败";
            }
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量保存
     *
     * @param map
     * @return
     */
    @PostMapping("/bashSave")
    public RestResponse bashSave(@RequestBody Map<String, Object> map) {
        RestResponse restResponse = null;
        try {
            buildingVulnerabilityService.bashSave(map);
            restResponse = RestResponse.succeed("批量保存成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if (StringUtils.isBlank(errormessage)) {
                errormessage = "批量保存失败";
            }
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导出
     *
     * @param response
     */
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, BuildingVulnerabilityQueryParams queryParams) {
        try {
            buildingVulnerabilityService.exportExcel(response, queryParams);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
        }
        ///return restResponse;
    }

    /**
     * 获取当前登陆人所在省市，以及判断当前登陆人属于国家级和省级管理员
     *
     * @param
     * @return
     */
    @GetMapping("/getLoginPersonInfo")
    public RestResponse getLoginPersonInfo() {
        RestResponse restResponse = null;
        try {
            Map<String, Object> map = buildingVulnerabilityService.getLoginPersonInfo();
            restResponse = RestResponse.succeed(map);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据省市县查询易损性数据
     *
     * @param
     * @return
     */
    @GetMapping("/getAllVulnerabilityOrder")
    public RestResponse getAllVulnerabilityOrder(@RequestParam String province, @RequestParam String city, @RequestParam String county) {
        List<BuildingVulnerability> allVulnerabilityOrder = buildingVulnerabilityService.getAllVulnerabilityOrder(province, city, county, "1");
        return ResponseCreator.createSuccessResponse(allVulnerabilityOrder);
    }

    /**
     * 根据省市县查询易损性数据
     *
     * @param
     * @return
     */
    @GetMapping("/remoteGetAllVulnerabilityOrder")
    public RestResponse remoteGetAllVulnerabilityOrder(@RequestParam String province, @RequestParam String city, @RequestParam String county, @RequestParam String source) {
        List<BuildingVulnerability> allVulnerabilityOrder = buildingVulnerabilityService.remoteGetAllVulnerabilityOrder(province, city, county, "1", source);
        return ResponseCreator.createSuccessResponse(allVulnerabilityOrder);
    }

    /**
     * 删除易损性
     *
     * @param ids
     * @return
     */
    @PostMapping("delbuildingVulnerability/{ids}")
    public RestResponse<String> delbuildingVulnerability(@PathVariable("ids") String ids) {
        try {
            log.debug("param:{}", ids);
            String[] split = ids.split(",");
            buildingVulnerabilityService.delbuildingVulnerability(Arrays.asList(split));
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @GetMapping("delbuildingVulnerability")
    public RestResponse<String> delbuildingVulnerability1(@RequestParam String ids) {
        try {
            log.debug("param:{}", ids);
            String[] split = ids.split(",");
            buildingVulnerabilityService.delbuildingVulnerability(Arrays.asList(split));
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

}
